package cn.yoaket.draw;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Polygon;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class DrawMap {
    final int WIDTH = 750;
    final int HEIGHT = 750;
    JFrame frame = new JFrame();
    List<Polygon> roads = null;
    List<Polygon> buildings = null;

    class DrawArea extends Canvas {
        @Override
        public void paint(Graphics g) {
            g.setColor(Color.BLUE);
            drawPoly(g, roads);

            g.setColor(Color.RED);
            drawPoly(g, buildings);


        }

        private void drawPoly(Graphics g, List<Polygon> polys) {
            if(polys != null){
                for (Polygon poly : polys) {
                    Coordinate[] coordinates = poly.getCoordinates();
                    for (int i = 0;i<coordinates.length-1;++i) {
                        g.drawLine((int)coordinates[i].getX(),(int)coordinates[i].getY(),(int)coordinates[i+1].getX(),(int)coordinates[i+1].getY());
                    }
                }
            }
        }
    }






    public void draw(List<Polygon> roads,List<Polygon> buildings){
        this.roads = roads;
        this.buildings = buildings;
        DrawArea area = new DrawArea();
        area.setSize(WIDTH,HEIGHT);

        frame.add(area);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
